/*
    This file is part of jTopaz.
    http://gcl.cis.udel.edu/projects/jtopaz
    Copyright (C) 2009 University of Delaware

    jTopaz is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    jTopaz is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of                                                        
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with jTopaz.  If not, see <http://www.gnu.org/licenses/>.
*/
import org.globus.ftp.*;
import org.globus.myproxy.MyProxy;
import org.ietf.jgss.GSSCredential;
import java.io.File;
import java.io.RandomAccessFile;

public class MarkerTest{
    
    public static void main(String[] args) {

	try {
	    MyMarkerListener listener = new MyMarkerListener();

	    GridFTPClient client = new GridFTPClient("rocks-97.sdsc.edu", 2811);
	    GridFTPClient client2 = new GridFTPClient("rocks-97.sdsc.edu", 2811);
	    MyProxy myProxyServer = new MyProxy("grid-devel1.rocksclusters.org", 7512);
	    GSSCredential cred = myProxyServer.get("flujul@sdsc.edu", "bobnewhart", 1000);

	    client.authenticate(cred);
	    client2.authenticate(cred);
	    System.out.println("authenticated\n");

	    String command = args[0];
	    
	    if(command.compareTo("-d") == 0) {
		System.out.println("download " + args[1]);
		File localFile = new File("/home/pmcclory/" + args[1]);
		
		if(! localFile.exists()) {
		    localFile.createNewFile();
		}
		else {
		    localFile.delete();
		    localFile.createNewFile();
		}
		RandomAccessFile raFile = new RandomAccessFile(localFile, "rw");

		FileRandomIO sink = new FileRandomIO(raFile);
		long size = client.getSize("/home/patmcclory/" + args[1]);
		System.out.println("size: " + size);

		double progress = 0;
		LocalFileMonitor monitor = new LocalFileMonitor("/home/pmcclory/" + args[1], size);

		DownloadThread transfer = new DownloadThread(client, localFile, "/home/patmcclory/" + args[1]);
	       
		transfer.start();
		monitor.run();

		while(!transfer.transferComplete()) {
		    System.out.println("Current Speed: " + monitor.getSpeed());
		    System.out.println("Current Progress: " + monitor.getProgress() + "%");
		    //System.out.wait(5000);
		    new Object().wait(1000);
		}
		monitor.stop();
		System.out.println("progress: " + progress);
		System.out.println("transfer complete");
	    }

	    else if (command.compareTo("-u") == 0) {
		System.out.println("upload " + args[1]);
		File localFile = new File(args[1]);
		RandomAccessFile raFile = new RandomAccessFile(localFile, "rw");
		
		FileRandomIO source = new FileRandomIO(raFile);
		if(client.exists(localFile.getName())) {
		    client.deleteFile(localFile.getName());
		}

		RemoteFileMonitor monitor = new RemoteFileMonitor(localFile.getName(), localFile.length(), client2);

		monitor.run();
		client.extendedPut(localFile.getName(), source, listener);
	    
		monitor.stop();

		System.out.println("transfer complete");
	    }
	    else {
		System.out.println("no command");
	    }
	    client.close();
	}
	catch (Exception e) {
	    System.out.println(e);
	}
    }
}